/* ==================================================================   
 * Created 2013-10-25上午11:43:16 by 郑伟
 * ==================================================================  
 *  EMV  Level2
 * Copyright (c) SZZCS S&T Co.ltd ShenZhen, 2012-2013 
 * ================================================================== 
 * 深圳市中磁计算机技术有限公司拥有该文件的使用、复制、修改和分发的许可权
 * 如果你想得到更多信息，请访问 <http://www.szzcs.com/cn>
 *
 * SZZCS S&T Co.ltd ShenZhen owns permission to use, copy, modify and 
 * distribute this documentation.
 * For more information on EMV, please 
 * see <http://www.szzcs.com/cn>.  
 * ================================================================== 
 */
package com.hangup.activity;

import com.devil.bbsclient.R;
import com.hangup.net.ActivityKeys;
import com.hangup.net.HttpUtil;
import com.hangup.pojo.Student;
import com.hangup.util.JsonUtil;
import com.hangup.util.NetWorkUtil;
import com.hangup.util.PublicUtil;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends Activity {
	private Button btBack;
	private Button btRegister;
	private Button btLogin;
	private TextView tvRegister;
	private EditText nameT;
	private EditText passT;

	private Dialog dialog;
	private String url;
	private String name_;
	private String pass_;
	private String result;
	private Student stu = new Student();
	PublicApplication pa;
	Handler handler_ = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case 0:
				Toast.makeText(LoginActivity.this, R.string.net,
						Toast.LENGTH_SHORT).show();
				break;
			case 1:
				dialog = NetWorkUtil.showDialog(LoginActivity.this, null, "登录",
						"登录中,请稍等");
				dialog.show();
				break;
			case 2:
				dialog.cancel();
				Toast.makeText(LoginActivity.this, "连接服务器异常!",
						Toast.LENGTH_LONG).show();
				break;
			case 3:
				dialog.cancel();
				Toast.makeText(LoginActivity.this, R.string.login_failure,
						Toast.LENGTH_LONG).show();
				break;
			case 4:
				// 登录成功
				nameT.setText("");
				passT.setText("");
				pa.setLogin_flag(true);
				Intent intent = new Intent();
				intent.setClass(LoginActivity.this, MainActivity.class);
				dialog.cancel();
				startActivity(intent);
				break;
			default:
				break;
			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		btBack = (Button) findViewById(R.id.btBack);
		btBack.setOnClickListener(new loginBack());

		btRegister = (Button) findViewById(R.id.btRegister);
		btRegister.setOnClickListener(new register());

		tvRegister = (TextView) findViewById(R.id.register_link);
		tvRegister.setOnClickListener(new register());

		btLogin = (Button) findViewById(R.id.signin_button);
		btLogin.setOnClickListener(new login());

		pa = (PublicApplication) getApplicationContext();
		SysApplication.getInstance().addActivity(this);

	}

	private final class loginBack implements OnClickListener {
		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			finish();
		}
	}

	private final class register implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(LoginActivity.this,
					RegisterActivity.class);
			startActivity(intent);
		}
	}

	private final class login implements OnClickListener {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			nameT = (EditText) findViewById(R.id.username_edit);
			passT = (EditText) findViewById(R.id.password_edit);
			name_ = nameT.getText().toString();
			pass_ = passT.getText().toString();
			if (name_ == null || name_.equals("")) {
				PublicUtil.setFocus(nameT);
				nameT.setError("用户名不能为空!");
			} else if (pass_ == null || pass_.equals("")) {
				PublicUtil.setFocus(passT);
				passT.setError("密码不能为空!");
			} else {
				if (NetWorkUtil.NetType(LoginActivity.this) == null) {
					handler_.sendMessage(handler_.obtainMessage(0));
					return;
				}
				handler_.sendMessage(handler_.obtainMessage(1));
				login();
			}
		}
	}

	// 登录线程
	protected void login() {
		url = ActivityKeys.BASE_URL + ActivityKeys.LOGIN_URL + "name=" + name_
				+ "&pass=" + pass_;
		new Thread(new Runnable() {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (!NetWorkUtil.webServer()) {
					handler_.sendMessage(handler_.obtainMessage(2));
					return;
				}
				result = HttpUtil.getHttpPostResultForUrl(url);
				if (result == null) {// 登录失败
					handler_.sendMessage(handler_.obtainMessage(3));
					return;
				} else if (result.equals(ActivityKeys.NET_FAILURE)) {
					handler_.sendMessage(handler_.obtainMessage(3));
				} else {
					stu = JsonUtil.parse(result, Student.class);
					pa.setStu(stu);
					handler_.sendMessage(handler_.obtainMessage(4));
				}
			}
		}).start();
	}
}
